"""
 encoding    : utf-8
 @File       : bu_men_lie_biao.py
 @Author     : jyy
 @Version    :
 @Description:只有正向验证方法类：tian_jia,bian_ji,shan_cu。验证方式为执行完成后再次获取页面信息核对，没有用数据库信息校验
 @Date       : 2024/07/15
"""


from cloud_test.auto_code.pages.system_mangage import den_lu
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import pytest
from selenium.webdriver.common.keys import Keys
import time
import copy


def get_xia_la_kuang(driver,mod='di_qu'):                           # 获取下拉框中的元素，传入浏览器实例，执行模式：mod='di_qu'（获取所属地区的下拉框选项）mod='can_ping'（获取绑定产品的下拉框选项）
    driver.implicitly_wait(5)                                       # 静态等待最多5秒
    int_a = 0                                                       # 决定了定位的是地区下拉框还是产品下拉框
    if mod == 'can_ping' :                                          # 定位产品下拉框
        int_a = 4
    elif mod == 'di_qu' :                                           # 定位地区下拉框
        int_a = 3
    try :
        web_yuan_su = driver.find_element(By.XPATH,f'/html/body/div[{int_a}]/div/div')                      # 获取所属地区下拉框中的全部地区
        list_a = web_yuan_su.text.split('\n')                       # 获取地区可选项名称,储存为列表
        return list_a                                               # 获取所有地区选项,储存为列表
    except:
        raise pytest.fail(reason='公司列表-编辑弹窗-获取所有地区选项失败，Xpath定位不到ul下面的li元素')


def get_yuan_su(driver):                                            # 获取页面列表信息
    driver.implicitly_wait(5)                                       # 静态等待最多5秒
    try :
        web_yuan_su = driver.find_element(By.XPATH,
            '/html/body/div[1]/section/section/main/div/div[2]/div/div/div/div/div/div/div/table/tbody')  # 获取页面列表中的全部部门信息
        list_a = web_yuan_su.text.split('\n')                       # 获取部门名称和编号,储存为列表

    except:
        raise pytest.fail(reason='部门列表-获取元素失败，Xpath定位不到tbody下面的tr元素')
    return list_a
def da_kai_lie_biao(driver):                                        # 进入部门列表页面
    try :
        driver.implicitly_wait(5)                                   # 静态等待最多5秒
        ele = driver.find_element(By.XPATH, '/html/body/div/section/aside/div/ul/li[4]/div/span')    # 定位系统管理菜单栏
        ele.click()                                                 # 点击系统管理菜单栏
        ele = driver.find_element(By.XPATH, '/html/body/div[1]/section/aside/div/ul/li[4]/ul/li[3]/a')  # 定位部门列表菜单栏
        ele.click()                                                 # 点击部门列表理菜单栏
    except :
        raise pytest.fail(reason='打开部门列表失败')



class bu_men():
    def tian_jia(self, driver, name):                               # 传入浏览器实例，要添加的部门名称
        self.driver = driver                                        # 获取浏览器实例
        self.driver.implicitly_wait(5)                              # 静态等待最多5秒
        da_kai_lie_biao(self.driver)                                # 打开部门列表

        time.sleep(0.5)
        list_a = copy.deepcopy(get_yuan_su(driver))                 # 获取添加前的列表信息

        ele = driver.find_element(By.XPATH, '/html/body/div[1]/section/section/main/div/div[1]/button[1]')   # 定位添加按钮
        ele.click()                                                 # 点击添加按钮

        ele = driver.find_element(By.XPATH, '//*[@id="department"]')# 定位输入框
        ele.click()                                                 # 点击输入框
        ele.send_keys(name)                                         # 输入添加的部门名称

        ele = driver.find_element(By.XPATH, '//*[@id="cityId"]')    # 定位所属地区下拉框
        ele.click()                                                 # 点击下拉框
        ele = driver.find_element(By.XPATH, '/html/body/div[3]/div/div/div/ul/li[1]')                       # 定位下拉框列表中第一个元素
        di_qu = ele.text                                            # 获取选择的地区的名称
        ele.click()                                                 # 点击第一个选项

        ele = driver.find_element(By.XPATH, '//*[@id="productId"]') # 定位绑定产品下拉框
        ele.click()                                                 # 点击下拉框
        ele = driver.find_element(By.XPATH, '/html/body/div[4]/div/div/div/ul/li[2]')                       # 定位下拉框列表中第2个元素
        can_ping = ele.text                                         # 获取选择的产品的名称
        ele.click()                                                 # 点击第一个选项

        ele = driver.find_element(By.XPATH,
                                  '/html/body/div[2]/div/div[2]/div/div[2]/div[3]/button')                  # 定位弹窗的添加按钮
        ele.click()                                                 # 点击弹窗的添加按钮

        try :
            ele = driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[3]/button')     # 定位弹窗的添加按钮
            raise pytest.fail(reason=f'已经添加过该公司')
        except :
            list_b = get_yuan_su(driver)                            # 获取添加后的列表信息
            if list_a == list_b :                                   # 对比两次的读取结果
                raise pytest.fail(reason=f'部门列表-添加失败\n添加前读取的公司列表信息:{list_a}\n添加后读取的部门列表信息:{list_b}')
            else :
                list_b.reverse()                                    # 翻转添加后的列表顺序
                b = list_b[0].split(' ')                            # 获取list_b的第一个元素。将编号和城市名称按空格拆分加入列表b
                if name == b[1] and di_qu == b [3] and can_ping == b [2]:                 # 对比添加的名称与读取到的列表中的新名称是否相等
                    assert 1 == 1 , '部门名称添加成功'
                else :
                    raise pytest.fail(reason=f'部门列表-添加失败，新名称与添加的名称不同\n添加前读取的部门列表信息:{list_a}\n添加后读取的部门列表信息:{list_b}')

    def bian_ji(self,driver,name,nwe,di_qu,can_ping):               # 传入浏览器实例，要编辑的部门名称，名称修改为nwe传入值，地区修改为di_qu传入值，绑定产品修改为can_ping
        self.driver = driver                                        # 获取浏览器实例
        self.driver.implicitly_wait(5)                              # 静态等待最多5秒
        da_kai_lie_biao(driver)                                     # 打开部门列表

        time.sleep(0.5)
        list_a = copy.deepcopy(get_yuan_su(driver))                 # 获取编辑前的列表信息

        int_a = 1                                                   # 变化选择的是第几个勾选框，从1开始
        int_b = 0                                                   # 记录找到的信息在第几行，没改变就是没找到
        int_di_qu = 1                                               # 定位地区选项用
        int_can_ping = 1                                            # 定位产品绑定选项用

        for i in list_a:
            if i.split(' ')[1] == name:
                ele = driver.find_element(By.XPATH,
                                          f'/html/body/div[1]/section/section/main/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr[{int_a}]/td[1]/span/label/span/input')
                # 定位要修改的部门的勾选框
                ele.click()                                         # 点击勾选框
                int_b = int_a - 1
                break
            int_a += 1
        if int_b == 0:
            raise pytest.fail(reason=f'部门列表-编辑失败，找不到：为{name}的部门名称\n编辑前读取的部门列表信息:{list_a}')

        ele = driver.find_element(By.XPATH, '/html/body/div[1]/section/section/main/div/div[1]/button[2]')   # 定位编辑按钮
        ele.click()                                                 # 点击编辑按钮

        ele = driver.find_element(By.XPATH,'//*[@id="department"]') # 定位部门名称输入框
        ele.click()                                                 # 点击输入框
        ele.send_keys(Keys.CONTROL, 'a')                            # 全选文本
        ele.send_keys(Keys.BACK_SPACE)                              # 删除原来的文本
        ele.send_keys(nwe)                                          # 输入要修改的城市名称


        ele = driver.find_element(By.XPATH, '//*[@id="cityId"]')    # 定位所属地区下拉框
        ele.click()                                                 # 点击下拉框

        list_di_qu = copy.deepcopy(get_xia_la_kuang(driver,'di_qu'))                                    # 获取所属地区下拉框中的可选项

        for i in list_di_qu :
            if i == di_qu :
                ele = driver.find_element(By.XPATH, f'/html/body/div[3]/div/div/div/ul/li[{int_di_qu}]')     # 定位下拉框列表中与di_qu同名的元素
                ele.click()                                         # 点击选项
                break
            int_di_qu += 1
        if int_di_qu > len(list_di_qu) :
            raise pytest.fail(reason=f'所属地区中没有名为：{di_qu}的地区\n可选地区为:{list_di_qu}')



        ele = driver.find_element(By.XPATH, '//*[@id="productId"]') # 定位绑定产品下拉框
        ele.click()                                                 # 点击下拉框

        list_can_ping = copy.deepcopy(get_xia_la_kuang(driver,'can_ping'))                              # 获取绑定产品下拉框中的可选项

        for i in list_can_ping :
            if i == can_ping :
                ele = driver.find_element(By.XPATH, f'/html/body/div[4]/div/div/div/ul/li[{int_can_ping}]')  # 定位下拉框列表中与int_can_ping同名的元素
                ele.click()                                         # 点击选项
                break
            int_can_ping += 1
        if int_can_ping > len(list_can_ping) :
            raise pytest.fail(reason=f'绑定产品中没有名为：{can_ping}的产品\n可选产品为:{list_can_ping}')

        ele = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div/div[2]/div[3]/button')        # 定位弹窗的编辑按钮
        ele.click()                                                 # 点击弹窗的编辑按钮

        list_b = get_yuan_su(driver)                                # 获取编辑后的列表信息

        if list_b[int_b].split(' ')[1] == nwe and list_b[int_b].split(' ')[2] == can_ping and list_b[int_b].split(' ')[3] == di_qu :
            assert 1 == 1 , '编辑成功'
        else :
            raise pytest.fail(reason=f'编辑失败，选择编辑的部门名：{name}修改为：{nwe}\n地区修改为：{di_qu}\n产品修改为：{can_ping}'
                                     f'\n编辑前的部门列表信息：{list_a}'
                                     f'\n编辑后的部门列表信息：{list_b}')

    def shan_chu(self,driver,name):                                 # 传入浏览器实例，要删除的部门名称，部门名称不能为空
        self.driver = driver                                        # 获取浏览器实例
        self.driver.implicitly_wait(5)                              # 静态等待最多5秒
        da_kai_lie_biao(self.driver)                                # 打开公司列表

        time.sleep(0.5)
        list_a = copy.deepcopy(get_yuan_su(driver))                 # 获取删除前的列表信息
        if list_a == [''] :
            raise pytest.fail(reason=f'没有可删除的公司信息，获取到的页面信息为：{list_a}')

        int_a = 1
        int_b = 0

        for i in list_a:
            if i.split(' ')[1] == name:
                ele = driver.find_element(By.XPATH,
                                          f'/html/body/div[1]/section/section/main/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr[{int_a}]/td[1]/span/label/span/input')
                                                                    # 定位要修改的城市的勾选框
                ele.click()                                         # 点击勾选框
                int_b = int_a - 1
                break
            int_a += 1
        if int_b == 0:
            raise pytest.fail(reasom=f'删除失败，没有找到名为：{name}的部门名称')

        ele = driver.find_element(By.XPATH, '/html/body/div[1]/section/section/main/div/div[1]/button[3]')  # 定位删除按钮
        ele.click()                                                 # 点击删除

        ele = driver.find_element(By.XPATH, '/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[2]/button[2]')  # 定位删除的二次确认弹窗的确定按钮
        ele.click()                                                 # 点击确定

        list_b = get_yuan_su(driver)                                # 获取删除后的公司列表信息

        try:
            if (list_b[int_b].split(' ')[1] == name                                         # 判断删除前后的部门名称
                    and list_b[int_b].split(' ')[2] == list_a[int_b].split(' ')[2]          # 判断删除前后的产品名称
                    and list_b[int_b].split(' ')[3] == list_a[int_b].split(' ')[3] ):       # 判断删除前后的地区名称
                raise pytest.xfail(reason=f'删除失败，删除的公司名称：{name}\n删除后的公司列表信息：{list_b}')
            else :
                assert 1 == 1, '删除成功'
        except:                                                     # 删除最后一行，删除后读取到的行数低于读取前
            if (list_b[int_b-1].split(' ')[1] == name                                         # 判断删除前后的部门名称
                    and list_b[int_b-1].split(' ')[2] == list_a[int_b-1].split(' ')[2]          # 判断删除前后的产品名称
                    and list_b[int_b-1].split(' ')[3] == list_a[int_b-1].split(' ')[3] ):       # 判断删除前后的地区名称
                raise pytest.xfail(reason=f'删除失败，删除的公司名称：{name}\n删除后的公司列表信息：{list_b}')
            else:
                assert 1 == 1, '删除成功'



"""
#调试用
driver = webdriver.Firefox()  # 创建火候浏览器实例
url = 'http://127.0.0.1:82/cloud/#/open/login'
a=den_lu.deng_lu()
a.da_kai_llq(driver,url)
a.deng_lu(driver,'admin','123456','8888')
b = bu_men()
# b.bian_ji(driver,'呜呜呜','新部门','班德尔','智慧社区管理系统')
b.shan_chu(driver,'新部门')
"""
